package com.duowei.kitchenshow.httputils;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AlertDialog;
import android.util.Log;

import com.android.volley.VolleyError;
import com.duowei.kitchenshow.BuildConfig;
import com.duowei.kitchenshow.MainActivity;
import com.duowei.kitchenshow.R;
import com.duowei.kitchenshow.app.MyApp;
import com.duowei.kitchenshow.bean.Jycssz;
import com.duowei.kitchenshow.bean.Version;
import com.duowei.kitchenshow.event.HttpResponse;
import com.duowei.kitchenshow.fragment.Main2Fragment;
import com.duowei.kitchenshow.fragment.MainFragment;
import com.duowei.kitchenshow.fragment.UpdateFragment;
import com.duowei.kitchenshow.service.Net;
import com.duowei.kitchenshow.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;
import org.litepal.crud.DataSupport;

import java.lang.reflect.Type;
import java.util.List;

public class Post {
    private String sqlQuery;
    private Post() {
    }

    private static Post singleton = null;

    public static Post getInstance() {
        if (singleton == null) {
            singleton = new Post();
        }
        return singleton;
    }
    public void postCfpb(final String sql){
        this.sqlQuery=sql;
        DownHTTP.postVolley6(Net.url, sql, new VolleyResultListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
            @Override
            public void onResponse(String response) {
                if(response.equals("]")||response.equals("]]")){
                    return;
                }
                EventBus.getDefault().post(new HttpResponse(response,false));
            }
        });
    }

    public void postUpdate(String stytle,String xh,String sqlQuery){
        String sql="begin update cfpb set by10='"+stytle+"' where xh='"+xh+"' " +
                "end "+sqlQuery;
        this.sqlQuery=sqlQuery;
        DownHTTP.postVolley6(Net.url, sql, new VolleyResultListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ToastUtil.showToast(error+"");
            }

            @Override
            public void onResponse(String response) {
                EventBus.getDefault().post(new HttpResponse(response,false));
            }
        });
    }

    public void postUpdate(final int state,boolean showTable){
        String sql;
        if(showTable){
            sql="begin update cfpb set by10='"+state+"' where xmbh='"+ MainFragment.mXmbh+"' end "+sqlQuery;
        }else{
            sql="begin update cfpb set by10='"+state+"' where by1='"+ Main2Fragment.mCzmc+"' end "+sqlQuery;
        }
        DownHTTP.postVolley6(Net.url, sql, new VolleyResultListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ToastUtil.showToast(error+"");
            }

            @Override
            public void onResponse(String response) {
                if(state==0){
                    EventBus.getDefault().post(new HttpResponse(response,false));
                }else{
                    EventBus.getDefault().post(new HttpResponse(response,true));
                }
            }
        });
    }

    public void postJycssz(){
        String sql="select csbh,csmc from jycssz where csbh in(select fcsbh from jycssz)|";
        DownHTTP.postVolley6(Net.url, sql, new VolleyResultListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }

            @Override
            public void onResponse(String response) {
                if(response.equals("]")||response.toUpperCase().contains("ERROR")){
                    return;
                }
                Type type = new TypeToken<List<Jycssz>>() {}.getType();
                List<Jycssz> jycsszList = new Gson().fromJson(response, type);
                DataSupport.deleteAll(Jycssz.class);
                DataSupport.saveAll(jycsszList);
            }
        });
    }

    public void deleteHistory(){
        String sql1="delete from cfpb where datediff(day,xdsj,getdate())>=3|";
        String sql2="delete from cfpbywc where datediff(day,xdsj,getdate())>=7|";
        String sql3="delete from kdscall where datediff(day,xdsj,getdate())>=2|";
        DownHTTP.postVolley7(Net.url, sql1 + sql2+sql3, new VolleyResultListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
            @Override
            public void onResponse(String response) {
            }
        });
    }

    public synchronized void postCall(String sql, final boolean speak){
        DownHTTP.postVolley7(Net.url, sql, new VolleyResultListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
            @Override
            public void onResponse(String response) {
                if(response.contains("richado")){
                    if(speak){
                        ToastUtil.showToast("发送成功");
                    }
                }else{
                    ToastUtil.showToast(response);
                }
            }
        });
    }

    public void checkVersion(final Activity activity, final boolean show){
        String url="http://androidwww.wxdw.top:2233/androidauth/app/kitchenshow.txt";
        DownHTTP.getVolley(url, new VolleyResultListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ToastUtil.showToast(error+"");
            }

            @Override
            public void onResponse(String response) {
                final Version version = new Gson().fromJson(response, Version.class);
                if(Integer.parseInt(version.getVersionCode())> BuildConfig.VERSION_CODE){
                    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                    builder.setIcon(R.mipmap.logo);
                    builder.setTitle("发现新版本，是否升级？");
                    builder.setMessage(version.getMsg());
                    builder.setNegativeButton("暂不升级",null);
                    builder.setPositiveButton("立即升级", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            UpdateFragment updateFragment = UpdateFragment.newInstance(version.getUrl(),version.getName());
                            updateFragment.show(activity.getFragmentManager(),"升级");
                        }
                    });
                    builder.show();
                }else{
                    if(show){
                        ToastUtil.showToast("当前己是最新版本");
                    }
                }
            }
        });
    }
}
